import 'package:flutter/cupertino.dart' show CupertinoIcons;
import 'package:flutter/material.dart';

import '../../../config.dart';
import '../../resources/resources.dart';
import '../my_text.dart';

typedef MyItemTagTapCallback = void Function(dynamic tag);

enum MyItemType {
  next, //进入下一步
  options, //选项
  unfold, //展开
  sw, //开关
  empty, //空的
  custom, //自定义
}

enum DividingLineType {
  solid,
  dotted,
}

class MyItem extends StatelessWidget {
  const MyItem({
    Key? key,
    this.onTap,
    this.onTagTap,
    required this.title,
    this.tag,
    this.isOn = false,
    this.leftWidget,
    this.titleWidget,
    this.trailingWidget,
    this.subtitle,
    this.trailingText,
    this.type = MyItemType.next,
    this.dividingLine = true,
    this.i18n = false,
    this.dividingLineType = DividingLineType.solid,
    this.height = 60,
    this.color,
    this.padding,
    this.trailingTextColor,
    this.trailingTextSize = 16,
  }) : super(key: key);

  final MyItemTagTapCallback? onTagTap;
  final VoidCallback? onTap;
  final String title;
  final String? subtitle;
  final String? trailingText;
  final dynamic tag;
  final bool isOn;
  final Widget? leftWidget;
  final Widget? trailingWidget;
  final Widget? titleWidget;
  final MyItemType type;
  final bool dividingLine;
  final bool i18n;
  final DividingLineType dividingLineType;
  final double height;
  final Color? color;
  final EdgeInsets? padding;

  final Color? trailingTextColor;
  final double trailingTextSize;

  Widget trailing() {
    if (trailingWidget != null) {
      return trailingWidget!;
    }
    if (type == MyItemType.next) {
      return Icon(
        CupertinoIcons.right_chevron,
        color: JmConfig.sbuTextStyle.color,
        size: 18,
      );
    } else if (type == MyItemType.options) {
      if (isOn) {
        return Icon(CupertinoIcons.check_mark_circled_solid,
            color: JmColors.main);
      } else {
        return Container();
      }
    } else if (type == MyItemType.unfold) {
      return Icon(
        Icons.unfold_more,
        color: Colors.grey[400],
      );
    } else if (type == MyItemType.sw) {
      return Switch(
        value: isOn,
        onChanged: (_) {
          if (onTap != null) {
            onTap!();
          }
          if (onTagTap != null) {
            onTagTap!(tag);
          }
        },
      );
    } else if (type == MyItemType.custom) {
      if (trailingWidget != null) {
        return trailingWidget!;
      }
    }

    return Container();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: color ?? Colors.transparent,
      child: InkWell(
        //animation: MyInkWellAnimation.ink,
        onTap: onTap != null || onTagTap != null
            ? () {
                if (onTap != null) {
                  onTap!();
                }
                if (onTagTap != null) {
                  onTagTap!(tag);
                }
              }
            : null,
        child: Container(
          margin:
              const EdgeInsets.only(top: 0, left: 16.0, bottom: 0, right: 0),
          //padding: padding??const EdgeInsets.only(top: 12, bottom: 12),
          constraints:
              BoxConstraints(maxHeight: double.infinity, minHeight: height),
          width: double.infinity,

          child: Row(
            children: <Widget>[
              leftWidget != null
                  ? Container(
                      padding: const EdgeInsets.only(right: 10),
                      child: leftWidget,
                    )
                  : Container(),
              Expanded(
                child: Container(
                  constraints: BoxConstraints(maxHeight: double.infinity, minHeight: height),
                  decoration: BoxDecoration(
                    border: dividingLine
                        ? Border(
                            bottom: Divider.createBorderSide(context,
                                width: 0.6, color: JmColors.line),
                          )
                        : null,
                  ),
                  child: Row(
                    children: [
                      Expanded(
                        child: subtitle != null
                            ? Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  titleWidget ??
                                      MyText(
                                        title,
                                        //size: 16,
                                        textAlign: TextAlign.start,
                                        i18n: i18n,
                                      ),
                                  const SizedBox(height: 1.0),
                                  MyText(
                                    subtitle!,
                                    size: 14,
                                    i18n: i18n,
                                    color: JmConfig.sbuTextStyle.color,
                                    textAlign: TextAlign.start,
                                  ),
                                ],
                              )
                            : titleWidget ??
                                MyText(
                                  title,
                                  size: 16,
                                  //color: Color(0xFF666666),
                                  textAlign: TextAlign.start,
                                  i18n: i18n,
                                ),
                      ),
                      const SizedBox(width: 10.0),
                      trailingText != null
                          ? Container(
                              padding: const EdgeInsets.only(right: 4),
                              child: MyText(
                                trailingText!,
                                size: trailingTextSize,
                                color:
                                    trailingTextColor ?? JmConfig.sbuTextStyle.color,
                                textAlign: TextAlign.start,
                              ),
                            )
                          : Container(),
                      //Spacer(),
                      trailing(),
                      gaph16,
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
